package com.itheima.test04;

import java.io.*;
import java.net.Socket;

//本地  读文件 向服务器上传  获取服务器的响应
public class ClientDemo {

    public static void main(String[] args) throws IOException {
        //创建 客户端对象
        Socket socket = new Socket("127.0.0.1", 8888);

        //创建本地输入流
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\Users\\Administrator\\Desktop\\沁儿.jpg"));
        //获取 输出流
        OutputStream os = socket.getOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(os);
        //边读边写
        byte[] bytes = new byte[1024];
        int len;
        while ((len = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, len);
        }
        bos.flush();
        //写完给一个结束标记
        socket.shutdownOutput();

        //获取输入流
        InputStream is = socket.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        //读取 服务器给的响应
        String s;
        while ((s=reader.readLine())!=null){
            System.out.println(s);
        }

        //各种释放资源
        bis.close();
        socket.close();

    }
}
